package com.bilibili.juc.interrupt;

/**
 * @ClassName: InterruptDemo5
 * @Author: Donnie
 * @Date: 2022/7/3 11:04
 * @Description:
 *      from: https://blog.csdn.net/qq_39385118/article/details/106659446
 */
public class InterruptDemo5 {
    public static void main(String[] args) {
        try {
            Thread thread = new Thread(() -> {
                try{
                    for(int i = 0; i < Integer.MAX_VALUE; i++){
                        if(Thread.interrupted()){
                            System.out.println("已经中断，运行次数为：" + i);
                            throw new InterruptedException();
                        }
                    }
                } catch(InterruptedException e){
                    System.out.println("线程catch 方法");
                }
            });

            thread.start();

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            thread.interrupt();
        } catch (Exception e) {
            System.out.println("main catch方法");
        }

    }
}
